/**
 * Copyright (c) 2018 苍穹跃龙
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.l.web.master.system.controller;


import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.l.core.web.RestCode;
import com.l.core.web.RestJson;
import com.l.web.master.system.entity.config.GlobalConfig;
import com.l.web.master.system.service.ISysConfigService;

/**
 * <p>
 * 配置 前端控制器
 * </p>
 *
 * @author 严秋旺
 * @since 2018-10-08
 */
@Controller
@RequestMapping("/master/system/config")
public class SysConfigController {

    @Autowired
    private ISysConfigService sysConfigService;

    @RequestMapping({ "/", "index" })
    public String index(Model model) {
        GlobalConfig globalConfig = sysConfigService.findGlobalConfig();
        model.addAttribute("globalConfig", globalConfig);
        return "master/system/config/index";
    }

    @RequestMapping("save")
    @ResponseBody
    public RestJson save(@Valid GlobalConfig globalConfig, Errors errors) {
        if (errors.hasErrors()) {
            return new RestJson(RestCode.FORM_BAD, errors);
        }

        sysConfigService.saveGlobalConfig(globalConfig);
        return RestJson.Success();
    }

}
